package com.enuo.utils;

import java.io.Serializable;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @ClassName: AjaxMsg
 * @Description: TODO(Utils - Ajax)
 * @author Charles.Tao
 * @date 2016年3月28日 上午11:08:58
 * @version 1.0
 */
public class AjaxMsg implements Serializable
{
    /**
     * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
     */
    private static final long serialVersionUID = -5905919145399289665L;

    private JSONObject json = new JSONObject();

    public AjaxMsg(boolean success)
    {
        this.json.put("data", "");
        this.json.put("success", Boolean.valueOf(success));
    }

    public AjaxMsg(boolean success, String msg)
    {
        this.json.put("data", msg);
        this.json.put("success", Boolean.valueOf(success));
    }

    public AjaxMsg(boolean success, long totalCount, JSONArray listObj)
    {
        this.json.put("data", listObj);
        this.json.put("totalCount", totalCount);
        this.json.put("success", Boolean.valueOf(success));
    }

    public AjaxMsg(boolean success, long totalCount, Object data)
    {
        this.json.put("data", data);
        this.json.put("totalCount", totalCount);
        this.json.put("success", Boolean.valueOf(success));
    }

    public AjaxMsg(boolean success, JSONObject data)
    {
        if (data != null)
        {
            this.json.put("data", data);
        }
        else
        {
            this.json.put("data", "");
        }

        this.json.put("success", Boolean.valueOf(success));
    }

    public AjaxMsg(boolean success, JSONArray data)
    {
        if (data != null)
        {
            this.json.put("data", data);
        }
        else
        {
            this.json.put("data", "");
        }

        this.json.put("success", Boolean.valueOf(success));
    }

    public AjaxMsg(boolean success, Object data)
    {
        if (data != null)
        {
            this.json.put("data", data);
        }
        else
        {
            this.json.put("data", "");
        }

        this.json.put("success", Boolean.valueOf(success));
    }

    public void put(String key, Object value)
    {
        this.json.put(key, value);
    }

    public static String result(boolean success)
    {
        return new AjaxMsg(success).toString();
    }

    public static String result(boolean success, String successMsg,
            String failedMsg)
    {
        return new AjaxMsg(success, success ? successMsg : failedMsg)
                .toString();
    }

    public static String success()
    {
        return new AjaxMsg(true).toString();
    }

    public static String success(String msg)
    {
        return new AjaxMsg(true, msg).toString();
    }

    public static String success(String msg, Map<String, Object> map)
    {
        return new AjaxMsg(true, msg).put(map);
    }

    public static String failed()
    {
        return new AjaxMsg(false).toString();
    }

    public static String sessionTimeout()
    {
        AjaxMsg ajaxMessage = new AjaxMsg(false);
        ajaxMessage.put("timeout", Boolean.valueOf(true));
        return ajaxMessage.toString();
    }

    public static String failed(String msg)
    {
        return new AjaxMsg(false, msg).toString();
    }

    public static String failed(String msg, Map<String, Object> map)
    {
        return new AjaxMsg(false, msg).put(map);
    }

    private String put(Map<String, Object> map)
    {
        for (Map.Entry<String, Object> entry : map.entrySet())
        {
            this.json.put(entry.getKey(), entry.getValue());
        }

        return this.toString();
    }

    public String toString()
    {
        return this.json.toString();
    }
}
